#define _CRT_SECURE_NO_WARNINGS 
#include "string.h"

namespace hb
{
	string::string(const char* str)
		:_size(strlen(str))
	{
		_capacity = _size;
		_str = new char[_capacity + 1];
		strcpy(_str, str);
	}

	/*string::string(const string& s)
		:_size(s._size)
		, _capacity(s._capacity)
	{
		_str = new char[s._capacity + 1];
		strcpy(_str, s._str);
	}*/

	string::string(const string& s)
		:_str(nullptr)
		,_size(0)
		,_capacity(0)
	{
		string tmp(s._str);
		swap(tmp);
	}

	//string& string::operator=(const string& s)
	//{
	//	if (this != &s)//?
	//	{
	//		char* tmp = new char[s._capacity + 1];
	//		strcpy(tmp, s._str);
	//		delete[] _str;

	//		_str = tmp;
	//		_size = s._size;
	//		_capacity = s._capacity;
	//	}

	//	return *this;
	//}

	string& string::operator=(string s)
	{
		swap(s);

		return *this;
	}

	string::~string()
	{
		delete[] _str;
		_str = nullptr;
		_size = _capacity = 0;
	}

	const char* string::c_str() const
	{
		return _str;
	}

	size_t string::size() const
	{
		return _size;
	}

	size_t string::capacity() const
	{
		return _capacity;
	}

	char& string::operator[](size_t pos)
	{
		assert(pos < _size);

		return _str[pos];
	}

	const char& string::operator[](size_t pos) const
	{
		assert(pos < _size);

		return _str[pos];
	}

	string::iterator string::begin()
	{
		return _str;
	}

	string::iterator string::end()
	{
		return _str + _size;
	}

	string::const_iterator string::begin() const
	{
		return _str;
	}
	string::const_iterator string::end() const
	{
		return _str + _size;
	}

	void string::push_back(char ch)
	{
		/*if (_size == _capacity)
		{
			size_t newcapacity = _capacity == 0 ? 4 : 2 * _capacity;
			reserve(newcapacity);
		}

		_str[_size] = ch;
		_size++;
		_str[_size] = '\0';*/

		insert(_size, ch);
	}

	void string::append(const char* str)
	{
		/*size_t len = strlen(str);
		if (_size + len > _capacity)
		{
			reserve(_size + len);
		}

		strcpy(_str + _size, str);
		_size += len;*/

		insert(_size, str);
	}

	void string::reserve(size_t n)
	{
		if (n > _capacity)
		{
			char* tmp = new char[n + 1];
			strcpy(tmp, _str);
			delete[] _str;

			_str = tmp;
			_capacity = n;
		}
	}

	void string::resize(size_t n, char ch)
	{
		if (n <= _size)
		{
			_str[n] = '\0';
			_size = n;
		}
		else
		{
			if (n > _capacity)
			{
				reserve(n);
			}

			while (_size < n)
			{
				_str[_size] = ch;
				_size++;
			}

			_str[_size] = '\0';
		}
	}

	void string::shrink_to_fit()
	{
		char* tmp = new char[_size + 1];
		strcpy(tmp, _str);
		delete[] _str;

		_str = tmp;
		_capacity = _size;
	}

	string& string::operator+=(char ch)
	{
		push_back(ch);

		return *this;
	}

	string& string::operator+=(const char* str)
	{
		append(str);

		return *this;
	}

	const size_t string::npos = -1;

	void string::insert(size_t pos, char ch)
	{
		assert(pos <= _size);

		if (_size == _capacity)
		{
			size_t newcapacity = _capacity == 0 ? 4 : 2 * _capacity;
			reserve(newcapacity);
		}

		for (size_t i = _size + 1; i > pos; i--)
		{
			_str[i] = _str[i - 1];
		}

		_str[pos] = ch;
		_size++;
	}

	//void string::insert(size_t pos, char ch)
	//{
	//	assert(pos <= _size);

	//	if (_size == _capacity)
	//	{
	//		size_t newcapacity = _capacity == 0 ? 4 : 2 * _capacity;
	//		reserve(newcapacity);
	//	}

	//	for (int i = _size; i >= (int)pos; i--)
	//	{
	//		_str[i + 1] = _str[i];
	//	}

	//	_str[pos] = ch;
	//	_size++;
	//}

	//void string::insert(size_t pos, const char* str)
	//{
	//	assert(pos <= _size);

	//	int len = strlen(str);
	//	if (_size + len > _capacity)
	//	{
	//		reserve(_size + len);
	//	}

	//	for (int i = _size; i >= (int)pos; i--)
	//	{
	//		_str[i + len] = _str[i];
	//	}

	//	strncpy(_str + pos, str, len);
	//	_size += len;
	//}

	void string::insert(size_t pos, const char* str)
	{
		assert(pos <= _size);

		size_t len = strlen(str);
		if (_size + len > _capacity)
		{
			reserve(_size + len);
		}

		/*for (size_t i = _size + len; i > pos + len - 1; i--)
		{
			_str[i] = _str[i - len];
		}*/
		size_t end = _size + len;
		while (end > pos + len - 1)
		{
			_str[end] = _str[end - len];
			--end;
		}

		strncpy(_str + pos, str, len);
		_size += len;
	}


	void string::erase(size_t pos, size_t len)
	{
		assert(pos < _size);

		if (len == npos || pos + len >= _size)//?
		{
			_size = pos;
			_str[_size] = '\0';
		}
		else
		{
			//strcpy(_str + pos, _str + pos + len);
			for (size_t i = pos + len; i <= _size ; i++)
			{
				_str[i - len] = _str[i];
			}
			_size -= len;
		}
	}

	size_t string::find(char ch, size_t pos)
	{
		for (size_t i = pos; i < _size; i++)
		{
			if (_str[i] == ch)
			{
				return i;
			}
		}

		return npos;
	}

	size_t string::find(const char* str, size_t pos)
	{
		const char* tmp = strstr(_str + pos, str);
		if (tmp != nullptr)
		{
			return tmp - _str;
		}

		return npos;
	}

	//string string::substr(size_t pos, size_t len)
	//{
	//	
	//	if (len == npos || pos + len >= _size)
	//	{
	//		string sub(_str + pos);
	//		return sub;

	//	}
	//	else
	//	{
	//		string sub;

	//		sub.reserve(len);
	//		for (size_t i = pos; i < pos + len; i++)
	//		{
	//			sub += _str[i];
	//		}

	//		return sub;
	//	}
	//}

	string string::substr(size_t pos, size_t len)
	{
		string s;

		size_t end = pos + len;

		if (len == npos || pos + len >= _size)
		{
			len = _size - pos;
			end = _size;
		}

		reserve(len);

		for (size_t i = pos; i < end; i++)
		{
			s += _str[i];
		}

		return s;
	}

	void string::swap(string& s)
	{
		std::swap(_str, s._str);
		std::swap(_size, s._size);
		std::swap(_capacity, s._capacity);
	}

	bool string::operator<(const string& s) const
	{
		return strcmp(_str, s._str) < 0;
	}
	bool string::operator==(const string& s) const
	{
		return strcmp(_str, s._str) == 0;
	}
	bool string::operator<=(const string& s) const
	{
		return *this < s || *this == s;
	}
	bool string::operator>(const string& s) const
	{
		return !(*this <= s);
	}
	bool string::operator>=(const string& s) const
	{
		return !(*this < s);
	}
	bool string::operator!=(const string& s) const
	{
		return !(*this == s);
	}

	void string::clear()
	{
		_str[0] = '\0';
		_size = 0;
	}

	ostream& operator<<(ostream& out, const string& s)
	{
		for (size_t i = 0; i < s.size(); i++)
		{
			cout << s[i];
		}

		return out;
	}

	istream& operator>>(istream& in, string& s)
	{
		s.clear();

		char ch = in.get();
		while (ch != ' ' && ch != '\n')
		{
			s += ch;
			ch = in.get();
		}

		return in;
	}

	istream& getline(istream& in, string& s)
	{
		s.clear();

		char ch = in.get();

		while (ch != '\n')
		{
			s += ch;
			ch = in.get();
		}

		return in;
	}
}

